home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / radio.c < prev    next >
C/C++ Source or Header  |  1996-08-01  |  4KB  |  171 lines

  1. #include <string.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include "diadef.h"
  6. #include "dialog.h"
  7.  
  8. static FIELD_RADIO *first;
  9.  
  10. /* #Specification: dialog / radio field
  11.     A radio field is like a check box, but many fields are
  12.     semanticly related. When you select one, the others are
  13.     unselected. radio field are used to present a set of
  14.     mutually exclusive option.
  15.  
  16.     It is possible to have a all radio fields unselected.
  17.  
  18.     Each radio field "edit" the save variable. Each one, when
  19.     selected will set the variable to its own instance value.
  20.     When all fields are unselected, the value 0xff is set.
  21.  
  22.     Here is an example
  23.     # 
  24.     The checkbox is presented like this
  25.  
  26.     #
  27.     Enable PPP ( ) at boot time
  28.                (o) on demand
  29.                ( ) never 
  30.     #
  31. */
  32. #
  33. PUBLIC FIELD_RADIO::FIELD_RADIO(
  34.     const char *_prompt,
  35.     char &_var,
  36.     char _instance_val,
  37.     const char *_title)
  38.     : FIELD_CHECK_RADIO (_prompt,_var,_title)
  39. {
  40.     instance_val = _instance_val;
  41.     next = first;
  42.     first = this;
  43. }
  44.  
  45.  
  46.  
  47. PUBLIC FIELD_RADIO::~FIELD_RADIO()
  48. {
  49.     // We remove ourself from the table
  50.     FIELD_RADIO **ptpt = &first;
  51.     while (*ptpt != NULL){
  52.         if (*ptpt == this){
  53.             *ptpt = next;
  54.             break;
  55.         }
  56.         ptpt = &(*ptpt)->next;
  57.     }
  58. }
  59.  
  60. /*
  61.     Draw only the input part of a field
  62. */
  63. PUBLIC void FIELD_RADIO::drawtxt (WINDOW *dialog)
  64. {
  65.     drawtxt_check (dialog,'(',')',val==instance_val ? 'o' : ' ');
  66. }
  67.  
  68. PRIVATE FIELD_RADIO *FIELD_RADIO::locate_key(char *key)
  69. {
  70.     FIELD_RADIO *pt = first;
  71.     while (pt != NULL){
  72.         if (&pt->var == &pt->var){
  73.             pt->format_htmlkey (key,0);
  74.             break;
  75.         }
  76.     }
  77.     return pt;
  78. }
  79.  
  80. PUBLIC void FIELD_RADIO::html_draw (int )
  81. {
  82.     char key[100];
  83.     FIELD_RADIO *original = locate_key (key);
  84.     html_printf ("<tr><td>%s<td>",prompt);
  85.     html_defvar ("radio",key,instance_val,val == instance_val ? "checked" : "");
  86.     html_printf ("\t\t%s\n",title);
  87.     if (original == this){
  88.         html_defvarcur (key,val);
  89.     }
  90. }
  91.  
  92. PUBLIC int FIELD_RADIO::html_validate(int )
  93. {
  94.     int ret = -1;
  95.     char key[100];
  96.     locate_key (key);
  97.     int oldval = atoi(html_getoldval(key));
  98.     int newval = atoi(html_getval(key));
  99.     if (val == oldval){
  100.         ret = 0;
  101.         val = newval;
  102.     }
  103.     return ret;
  104. }
  105.  
  106. PUBLIC void FIELD_RADIO::dokey (
  107.     WINDOW *,
  108.     int key,
  109.     FIELD_MSG &msg)
  110. {
  111.     /* #Specification: dialog / FIELD_MSG / strategy
  112.         The FIELD_MSG object was created to achieve the
  113.         radio button widget. When a radio button is selected
  114.         all other radio button operating on the same variable
  115.         should unset themselves. To achive this, the radio
  116.         button dokey() function loaded the address of the edition
  117.         variable into the msg and loaded the instance value into
  118.         the msg.int_val. It set msg.is_loaded to 1. This tells
  119.         the main dialog edit loop to call processmsg for each
  120.         field, passing to it the msg structure.
  121.  
  122.         Each field will compare msg.key to whatever it decide
  123.         uniquely identify itself. If there is a match, it will
  124.         process msg.int_val and decide whatever should be done.
  125.  
  126.         All radio buttons will redraw themselves if needed.
  127.  
  128.         This mecanism achieve a form of simple broadcast to all
  129.         fields of a dialog.
  130.     */
  131.     switch (key){
  132.     case ' ':
  133.         msg.is_loaded = 1;
  134.         msg.key = (void*)var;
  135.         if (val == instance_val){
  136.             msg.int_val = 0xff;
  137.         }else{
  138.             msg.int_val = instance_val;
  139.         }
  140.         break;
  141.     }
  142. }
  143.  
  144. PROTECTED void FIELD_RADIO::processmsg(
  145.     WINDOW *dialog,
  146.     FIELD_MSG &msg,
  147.     int drawok)
  148. {
  149.     if (msg.key == (void*)var){
  150.         int was_on = instance_val == val;
  151.         val = msg.int_val;
  152.         int is_on = instance_val == val;
  153.         if (drawok && was_on != is_on) drawtxt(dialog);
  154.     }
  155. }
  156.  
  157. /*
  158.     Add a check box field to the dialog.
  159. */
  160. PUBLIC FIELD_RADIO *DIALOG::newf_radio(
  161.     const char *prompt,
  162.     char &var,
  163.     char instance_val,
  164.     const char *title)
  165. {
  166.     FIELD_RADIO *s = new FIELD_RADIO(prompt,var,instance_val,title);
  167.     add (s);
  168.     return s;
  169. }
  170.  
  171.